﻿using Abp.BackgroundJobs;
using Abp.Dependency;
using System;
using System.Collections.Generic;
using System.Text;
using Abp.Domain.Repositories;
using FwProject.Core.SettlementInfoModel;
using System.Linq;
using Abp.Domain.Uow;
using Castle.Core.Logging;
using FwProject.PublicService.JPush;
using FwProject.Authorization.Users;
using FwProject.Core.Restaurant;
using FwProject.JPush.JPushModel;

namespace FwProject.BackGround.Settlement
{
    public class SettlementPayBackGround : BackgroundJob<BasArg>, ITransientDependency 
    {
        private readonly IRepository<SettlementInfo,Guid> _repositorySettlementInfo;
        private readonly IRepository<RestaurantUserInfo, Guid> _repositoryRestaurantUserInfo;
        public readonly IJPushAppservice _jPushAppservice;
        private readonly IRepository<User, long> _userRepository;
        public ILogger logger { get; set; }
        public SettlementPayBackGround(IRepository<SettlementInfo, Guid> repositorySettlementInfo, IJPushAppservice jPushAppservice,
            IRepository<User, long> userRepository,
            IRepository<RestaurantUserInfo, Guid> repositoryRestaurantUserInfo) {
            _repositoryRestaurantUserInfo = repositoryRestaurantUserInfo;
            _userRepository = userRepository; 
             _jPushAppservice = jPushAppservice;
            _repositorySettlementInfo = repositorySettlementInfo;
            logger = NullLogger.Instance;
        }
        [UnitOfWork]
        public override void Execute(BasArg args)
        {
            try
            {
                using (CurrentUnitOfWork.DisableFilter(AbpDataFilters.MayHaveTenant))
                {
                    logger.Info("后台任务通知收款人开始处理收款确认");
               
                        var usercar = _userRepository.Get(args.UserId);
                        ///获取餐厅信息
                        TypeSettlementPushMessageModel pushModel = new TypeSettlementPushMessageModel();
                        pushModel.TopicModel = JPush.JPushHelpel.TopicAppSystemDriver;
                        pushModel.UserId = usercar.Id;
                        pushModel.Id = Guid.Empty;
                        ///通知司机需要收款
                       _jPushAppservice.RestaurantPushMessage($"{usercar.Mobile}", pushModel, $"你有带处理的收款请及时处理。");

                   logger.Info($"后台任务通知收款人开始处理收款确认");
                    CurrentUnitOfWork.SaveChanges();
                }

            }
            catch (Exception exe)
            {

                logger.Error("后台任务通知收款人开始处理收款失败", exe);
            }
           
        }
    }
}
